package com.jiangjiang.eshop.order.price.v1.calculator.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jiangjiang.eshop.commodity.dto.GoodsSkuDTO;
import com.jiangjiang.eshop.commodity.service.CommodityService;
import com.jiangjiang.eshop.order.dto.OrderItemDTO;
import com.jiangjiang.eshop.order.dto.PromotionActivityResult;
import com.jiangjiang.eshop.order.price.v1.calculator.AbstractGiftPromotionActivityCalculator;
import com.jiangjiang.eshop.order.price.v1.calculator.PromotionActivityCalculator;
import com.jiangjiang.eshop.promotion.dto.PromotionActivityDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author Jianghaowen
 * @Date 2021/7/14
 * @Description: 赠品促销类型的促销活动的价格计算组件
 * @Version V1.0
 **/
@Component
public class DirectGiftPromotionActivityCalculator extends AbstractGiftPromotionActivityCalculator implements PromotionActivityCalculator {
    @Autowired
    private CommodityService commodityService;
    /**
     *
     * @param item 订单条目
     * @param promotionActivity 促销活动
     * @return
     * @throws Exception
     */
    @Override
    public PromotionActivityResult calculate(OrderItemDTO item, PromotionActivityDTO promotionActivity) throws Exception {

         // {“giftGoodsSkuIds”: [2, 340, 590]} 
        JSONObject jsonObject = JSONObject.parseObject(promotionActivity.getRule());
        PromotionActivityResult result = new PromotionActivityResult();
        List<Long> giftGoodsSkuIds = JSONObject.parseArray(jsonObject.getString("giftGoodsSkuIds"), Long.class);
        List<GoodsSkuDTO> goodsSkuByIds = commodityService.getGoodsSkuByIds(giftGoodsSkuIds);
        result.setOrderItems(createGiftOrderItems(goodsSkuByIds));
        return null;
    }
}
